package com.ktwlsoft.fundmanage.domain.business;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ktwlsoft.fundmanage.domain.entity.SkipConfig;
import com.ktwlsoft.fundmanage.domain.entity.SkipHistory;
import com.ktwlsoft.fundmanage.domain.repository.SkipConfigRepository;
import com.ktwlsoft.fundmanage.domain.repository.SkipHistoryRepository;
import com.ktwlsoft.fundmanage.mapper.ISkipConfigMapper;
import com.ktwlsoft.fundmanage.payload.BaseResult;
import com.ktwlsoft.fundmanage.payload.ResultType;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Service
public class SystemService {

    private final SkipConfigRepository skipConfigRepository;

    private final SkipHistoryRepository skipHistoryRepository;

    private final ISkipConfigMapper skipConfigMapper;

    public SystemService(SkipConfigRepository skipConfigRepository, SkipHistoryRepository skipHistoryRepository, ISkipConfigMapper skipConfigMapper) {
        this.skipConfigRepository = skipConfigRepository;
        this.skipHistoryRepository = skipHistoryRepository;
        this.skipConfigMapper = skipConfigMapper;
    }

    public boolean isSkip(SkipConfig.SystemType systemType, Date now) {
        SkipConfig skipConfig = skipConfigRepository.findBySystemType(systemType);
//        List<SkipHistory> skipHistories = skipHistoryRepository.findByCreatedAtLessThan(now);
//        if(skipHistories.size()>0){
//            skipHistories.sort((o1, o2) -> o2.getCreatedAt().compareTo(o1.getCreatedAt()));
//            return skipHistories.get(0).isSkip();
//        }else {
//            return skipConfig.isSkip();
//        }
        return skipConfig.isSkip();
    }

    @Transactional(rollbackOn = Exception.class)
    public SkipConfig saveSkipConfig(SkipConfig skipConfig) {
        skipConfig.setPassSkip(skipConfig.isSkip());
        SkipHistory skipHistory = new SkipHistory(skipConfig.isSkip(),skipConfig.getSystemType());
        skipHistoryRepository.save(skipHistory);
        return skipConfigRepository.save(skipConfig);
    }

    /**
     * 跳过配置-分页 on 2019/9/17 11:25
     *
     * @param pn         pn
     * @param pageSize   pageSize
     * @param systemType 系统类型
     * @return BaseResult<?>
     */
    public BaseResult<?> findSkipConfigPage(int pn, int pageSize, String systemType) {
        JSONObject jsonObject = new JSONObject();
        if (systemType != null && !"".equals(systemType.trim())) {
            jsonObject.put("systemType", systemType);
        }
        PageHelper.startPage(pn, pageSize);
        List<JSONObject> list = skipConfigMapper.findSkipConfigPage(jsonObject);
        return BaseResult.ok(new PageInfo<>(list));
    }

    /**
     * 跳过配置-详情 on 2019/9/17 11:34
     *
     * @param systemType systemType
     * @return BaseResult<?>
     */
    public BaseResult<?> getSkipConfigDetails(String systemType) {
        SkipConfig skipConfig = skipConfigMapper.getById(systemType);
        if (skipConfig == null) {
            return BaseResult.body(systemType, ResultType.BUSINESS_ERROR).withMessage("该数据不存在");
        }
        return BaseResult.ok(skipConfig);
    }

    /**
     * 跳过配置-是否跳过 on 2019/9/17 11:38
     *
     * @param systemType systemType
     * @return BaseResult<?>
     */
    @Transactional(rollbackOn = Exception.class)
    public BaseResult<?> skipConfigUpdateSkip(String systemType) {
        SkipConfig skipConfig = skipConfigMapper.getById(systemType);
        if (skipConfig == null) {
            return BaseResult.body(systemType, ResultType.BUSINESS_ERROR).withMessage("该数据不存在");
        }
        boolean skipOld = skipConfig.isSkip();
        skipConfig.setSkip(!skipConfig.isSkip());
        try {
            setSkipHistory(skipConfig.getSystemType(),skipOld);
            skipConfigRepository.save(skipConfig);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResult.body(systemType, ResultType.BUSINESS_ERROR);
        }
        return BaseResult.ok(skipConfig);
    }

    /**
     * 跳过配置-是否跳过 on 2019/9/17 11:38
     *
     * @param systemType systemType
     * @return BaseResult<?>
     */
    @Transactional(rollbackOn = Exception.class)
    public BaseResult<?> skipConfigUpdateSkip(String systemType, Boolean skip) {
        SkipConfig skipConfig = skipConfigMapper.getById(systemType);
        if (skipConfig == null) {
            return BaseResult.body(systemType, ResultType.BUSINESS_ERROR).withMessage("该数据不存在");
        }
        if (skip == null) {
            return BaseResult.body(systemType, ResultType.BUSINESS_ERROR).withMessage("skip为空");
        }
        boolean skipOld = skipConfig.isSkip();
        skipConfig.setSkip(skip);
        try {
            setSkipHistory(skipConfig.getSystemType(),skipOld);
            skipConfigRepository.save(skipConfig);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResult.body(systemType, ResultType.BUSINESS_ERROR);
        }
        return BaseResult.ok(skipConfig);
    }

    private void setSkipHistory(SkipConfig.SystemType systemType, boolean skip){
        SkipHistory skipHistory = new SkipHistory(skip,systemType);
        skipHistoryRepository.save(skipHistory);
    }
}
